//
//  NetWork.m
//  HelloFriend
//
//  Created by going-HNL on 16/3/25.
//  Copyright © 2016年 going-HNL. All rights reserved.
//

#import "NetWork.h"
#import "AFNetworking.h"
#import "KVNProgress.h"

@implementation NetWork

+ (void)registerCode:(NSString *)str  success:(SuccessBlock)suceess failed:(FailureBlock)failed{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?phone=%@",HttpMain,HttpCode,str];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        suceess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        failed(error);
    }];
}

+ (void)registerUser:(NSDictionary *)dic  success:(SuccessBlock)suceess failed:(FailureBlock)failed{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?name=%@&phone=%@&verifycode=%@&password=%@",HttpMain,HttpRegist,dic[@"name"],dic[@"phone"],dic[@"verifycode"],dic[@"password"]];
    //    NSString *urlStringUTF8 = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        suceess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        failed(error);
    }];
}

+ (void)login:(NSDictionary *)dic  success:(SuccessBlock)suceess failed:(FailureBlock)failed{
    [KVNProgress show];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?phone=%@&password=%@",HttpMain,HttpLogin,dic[@"phone"],dic[@"password"]];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        suceess(responseObject);
        [KVNProgress dismiss];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        failed(error);
        [KVNProgress dismiss];
    }];
}

+(void)infoSuccess:(SuccessBlock)suceess failed:(FailureBlock)failed{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?phone=%@&access_token=%@",HttpMain,HttpInfo,USER_DEFAULTGet(@"phone"),USER_DEFAULTGet(@"token")];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        suceess(responseObject);
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        failed(error);
    }];
}

+(void)headImageData:(NSData *)data andDic:(NSDictionary *)dic success:(SuccessBlock)suceess failed:(FailureBlock)failed{
    [KVNProgress show];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",HttpMain,HttpImage];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        /**
         *  appendPartWithFileURL   //  指定上传的文件
         *  name                    //  指定在服务器中获取对应文件或文本时的key
         *  fileName                //  指定上传文件的原始文件名
         *  mimeType                //  指定商家文件的MIME类型
         */
        [formData appendPartWithFileData:data name:@"Filedata" fileName:@"Filedata.jpg" mimeType:@"image/jpg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject[@"status"] intValue] == 1) {
            NSMutableArray *keyArr = @[].mutableCopy;
            NSMutableArray *valueArr = @[].mutableCopy;
            if ([dic[@"keys"] count] > 0) {
                [keyArr addObjectsFromArray:dic[@"keys"]];
            }
            
            if ([dic[@"values"] count] > 0) {
                [valueArr addObjectsFromArray:dic[@"values"]];
            }
            
            [keyArr addObject:@"avatar"];
            [valueArr addObject:responseObject[@"content"]];
            
////            NSString *keysStr = [keyArr componentsJoinedByString:@","];
////            NSString *valuesStr = [valueArr componentsJoinedByString:@","];
//            NSMutableDictionary *dic =[[NSMutableDictionary alloc]init];
//            [dic setObject:USER_DEFAULTGet(@"phone") forKey:@"phone"];
//            [dic setObject:USER_DEFAULTGet(@"token") forKey:@"access_token"];
            NSMutableArray *allArr = @[].mutableCopy;
            for (int i = 0; i < keyArr.count; i ++) {
                [allArr addObject:[NSString stringWithFormat:@"keys[]=%@&values[]=%@",keyArr[i],valueArr[i]]];
            }
            
            
            NSString *url = [NSString stringWithFormat:@"%@%@?phone=%@&access_token=%@&%@",HttpMain,HttpSetInfo,USER_DEFAULTGet(@"phone"),USER_DEFAULTGet(@"token"),[allArr componentsJoinedByString:@"&"]];
            
            [manager GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
                suceess(responseObject);
                [KVNProgress dismiss];
            } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
                failed(error);
                [KVNProgress dismiss];
            }];
        }else{
            UIViewController *result = nil;
            
            UIWindow * window = [[UIApplication sharedApplication] keyWindow];
            if (window.windowLevel != UIWindowLevelNormal)
            {
                NSArray *windows = [[UIApplication sharedApplication] windows];
                for(UIWindow * tmpWin in windows)
                {
                    if (tmpWin.windowLevel == UIWindowLevelNormal)
                    {
                        window = tmpWin;
                        break;
                    }
                }
            }
            
            UIView *frontView = [[window subviews] objectAtIndex:0];
            id nextResponder = [frontView nextResponder];
            
            if ([nextResponder isKindOfClass:[UIViewController class]])
                result = nextResponder;
            else
                result = window.rootViewController;
            [KVNProgress showErrorWithStatus:@"" onView:result.view];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failed(error);
        [KVNProgress dismiss];
    }];
}

+(void)setInfo:(NSDictionary *)dic success:(SuccessBlock)suceess failed:(FailureBlock)failed{
    [KVNProgress show];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@?phone=%@&access_token=%@&keys=%@&values=%@",HttpMain,HttpSetInfo,USER_DEFAULTGet(@"phone"),USER_DEFAULTGet(@"token"),dic[@"keys"],dic[@"values"]];
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        suceess(responseObject);
        [KVNProgress dismiss];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        failed(error);
        [KVNProgress dismiss];
    }];
}
@end
